package ynu.lcy.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.delievryAddress.AddDeliveryAddressRequestDTO;
import ynu.lcy.dto.delievryAddress.UpdateDeliveryAddressRequestDTO;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;

import ynu.lcy.entity.DeliveryAddress;
import ynu.lcy.service.DeliveryAddressService;
import ynu.lcy.vo.DeliveryAddressVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "配送地址接口")
@RestController
@CrossOrigin
@RequestMapping("/")
public class DeliveryAddressController  {

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private DeliveryAddressService service;
    /**
     * 用户地址列表
     * @param request 请求
     * @return 用户地址列表
     * */
    @GetMapping("/list")
    @ApiOperation("用户地址列表")
    public BaseResponse<List<DeliveryAddressVO>> list(HttpServletRequest request) {
        List<DeliveryAddress> list = service.list(request);
        return ResultUtils.success(list.stream().map(e -> modelMapper.map(e, DeliveryAddressVO.class)).toList());
    }

    /**
     * 根据id获取
     * @param id id
     * @return 用户地址列表
     * */
    @GetMapping("/get/{id}")
    @ApiOperation("根据id获取")
    public BaseResponse<DeliveryAddressVO> get(@PathVariable Long id) {
        DeliveryAddressVO vo = modelMapper.map(service.get(id), DeliveryAddressVO.class);
        return ResultUtils.success(vo);
    }

    /**
     * 添加
     * @param dto dto
     * @return 用户地址列表
     * */
    @PostMapping("/add")
    @ApiOperation("添加")
    public BaseResponse<DeliveryAddressVO> add(@RequestBody AddDeliveryAddressRequestDTO dto
            , HttpServletRequest request) {
        DeliveryAddress address = service.save(dto, request);
        return ResultUtils.success(modelMapper.map(address,DeliveryAddressVO.class));
    }

    /**
     * 修改
     * @param dto dto
     * @return 用户地址列表
     * */
    @PostMapping("/update")
    @ApiOperation("修改")
    public BaseResponse<Boolean> update(@RequestBody UpdateDeliveryAddressRequestDTO dto) {
        return ResultUtils.success(service.update(dto));
    }

    /**
     * 删除
     * @param id id
     * @return 用户地址列表
     * */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除")
    public BaseResponse<Boolean> add(@PathVariable Long id) {
        return ResultUtils.success(service.delete(id));
    }

}
